<template>
<div class="dashboard-container">
    <div class="app-container">
      <el-card shadow="never">
          <el-form label-width="80px" size="small">
            <el-row>
              <el-col :span="6">
                <el-form-item label="关键字">
                  <el-input style="200px" v-model="tableData.keyword" placeholder="根据编号搜索"></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="6" :offset="12">
                <el-form-item style="text-align:right">
                  <el-button @click="clear()">清除</el-button>
                  <el-button @click="filter()" type="primary">搜索</el-button>
                </el-form-item>
              </el-col>
            </el-row>
          </el-form>
          <!-- 消息提示框 -->
          <el-alert
            :title="bbq"
            type="info"
            class="alert"
            :closable="false"
            style="margin-bottom: 15px;"
            show-icon
          ></el-alert>
            <!-- 数据列表 -->
          <el-table
            :data="items"
            :index="indexMethod"
            style="width: 100%"
            :header-cell-style="{
            'background-color': '#fafafa',
            'border-bottom': '2px #e8e8e8 solid'}">
            <el-table-column
            prop="id"
            label="编号"
            width="220px">
          </el-table-column>
          <el-table-column
            prop="questionType"
            label="题型"
           width="80px">
            <template slot-scope="scope">
              {{questionType.find(item=>item.value===+scope.row.questionType).label}}
            </template>
          </el-table-column>
          <el-table-column
            prop="questionIDs"
            label="题目编号"
            width="220px">
            <template #default="{row}">
              <div v-for="item in row.questionIDs" :key="item.number">
                <a href="#" @click.prevent="openPreviewDialog(item)" style="color:#09f;">{{item.number}}</a>
              </div>
            </template>
          </el-table-column>
          <el-table-column
            prop="addTime"
            label="录入时间"
            width="180">
              <template #default="{row}">
              {{row.addTime|parseTimeByString}}
              </template>
          </el-table-column>
          <el-table-column
            prop="totalSeconds"
            label="答题时间(s)">
          </el-table-column>
          <el-table-column
            prop="accuracyRate"
            label="正确率（%）">
          </el-table-column>
          <el-table-column
            prop="userName"
            label="录入人">
          </el-table-column>
          <el-table-column
            prop=""
            label="操作"
            width="80px">
              <template #default="{row}">
                <el-button plain type="danger" size="small" circle icon="el-icon-delete" @click="delQuestion(row)" title="删除"></el-button>
              </template>
          </el-table-column>
        </el-table>
        <!-- 分页器 -->
        <div class="block">
          <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="tableData.page"
            :page-sizes="[20,30,50,100]"
            :page-size="tableData.pagesize"
            layout="prev, pager, next, sizes, jumper"
            :total="counts"
            background
            style="margin-top: 20px; text-align: right;">
          </el-pagination>
        </div>
      </el-card>
      <questions-preview ref="questionPreview" :data="questionInfo"></questions-preview>
    </div>
  </div>
</template>

<script>
import { randoms, removeRandoms } from '@/api/hmmm/questions.js'
import { difficulty, questionType } from '@/api/hmmm/constants'
import QuestionsPreview from '../components/questions-preview'
export default {
  data () {
    return {
      tableData: {
        page: 1,
        pagesize: 20,
        keyword: null
      },
      difficulty,
      questionType,
      items: [],
      counts: 0,
      questionInfo: {}
    }
  },
  computed: {
    bbq () {
      return `数据一共 ${this.counts} 条`
    }
  },
  components: {
    QuestionsPreview
  },
  created () {
    this.getres()
  },
  methods: {
    // 获取员工列表
    async getres () {
      const { data: { items, counts } } = await randoms(this.tableData)
      this.items = items
      this.counts = counts
    },
    // 当前页发生变化
    handleSizeChange (value) {
      this.tableData.pagesize = value
      this.tableData.page = 1
      this.getres()
    },
    // 当前页发生变化时
    handleCurrentChange (value) {
      this.tableData.page = value
      this.getres()
    },
    // 处理序号问题
    indexMethod (index) {
      return (this.tableData.page - 1) * this.tableData.pagesize + index + 1
    },
    clear () {
      this.tableData.keyword = null
    },
    filter () {
      this.tableData.page = 1
      this.getres()
    },
    async delQuestion (question) {
      await this.$confirm('此操作将永久删除该题组, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
      await removeRandoms({ id: question.id })
      this.$message.success('删除成功')
      this.getres()
    },
    openPreviewDialog (questionInfo) {
      this.questionInfo = questionInfo
      this.$nextTick(() => {
        this.$refs.questionPreview.open()
      })
    }
  }
}
</script>

<style lang='scss' scoped>
.dashboard-container{
  .app-container{
    margin: 10px 0;
    padding: 0 10px;
  }
}
::v-deep .el-pagination__jump {
  margin-left: 0;
}

</style>
